#include <stdio.h>
#include <stdlib.h>

#include "src/ltlexer.h"
#include "src/ltlog.h"
#include "src/lttry.h"

int
main(int argc, char** argv) {
    for (int i = 1; i < argc; ++i) {
        FILE* reader = fopen(argv[i], "r");

        if (reader == NULL) {
            ltlog_Log1(Error, "failed to open file", "path", String(argv[i]));
            return 1;
        }

        fseek(reader, 0, SEEK_END);
        long n = ftell(reader);
        fseek(reader, 0, SEEK_SET);

        char* content = malloc(n + 1);

        fread(content, n, 1, reader);
        content[n] = 0;

        ltlexer_Lexer lexer = {.content = content, .pos = 0};
        ltlexer_Token token;

        for (;;) {
            lttry_Try(ltlexer_Next(&lexer, &token));

            if (token.type == ltlexer_kEof)
                break;

            printf("%s:%" lt_FmtUSize "\t%.*s\n", argv[i], token.pos,
                   (int)token.len, lexer.content + token.pos);
        }
    }
    return 0;
}
